package com.pajk.user.service.credit;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.pajk.user.util.HttpsUtil;

/**
 * User: haihua.chenhh
 * Date: 14-4-14
 * Time: 上午10:48
 */
@Deprecated
public class WltHttpService {
    private static final Logger log = LoggerFactory.getLogger(WltHttpService.class);

    private String              keyStorePassword;
    private String              trustStorePassword;
    private SSLSocketFactory    sslSocketFactory;

    public void init() {
        System.setProperty("java.protocol.handler.pkgs", "sun.net.www.protocol");
        try {
            sslSocketFactory = HttpsUtil.getSSLSocketFactory("pkcs12", keyStorePassword,
                "META-INF/wlt/keyStore_stg.pfx", trustStorePassword,
                "META-INF/wlt/trustStore_stg.jks");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public Document call(String urlStr, String requestXmlParams) {
        String response = "";
        try {
             response = HttpsUtil.invoke(urlStr, requestXmlParams,sslSocketFactory);
            return DocumentHelper.parseText(response);
        } catch (DocumentException e) {
            log.error("reponse xml invalide!response:" + response + e.getMessage(), e);
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public void setKeyStorePassword(String keyStorePassword) {
        this.keyStorePassword = keyStorePassword;
    }

    public void setTrustStorePassword(String trustStorePassword) {
        this.trustStorePassword = trustStorePassword;
    }
}
